package LCR_code;

import Lee_code.TreeNode;

import java.util.*;

/**
 * @author blh
 * @date 2024年08月11日 16:02
 */
public class LCR_151_811 {
    public List<List<Integer>> decorateRecord(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        boolean left2right = true;
        while (!deque.isEmpty()) {
            List<Integer> tempRes = new ArrayList<>();
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode temp = deque.poll();
                tempRes.add(temp.val);
                if (temp.left != null) {
                    deque.offer(temp.left);
                }
                if (temp.right != null) {
                    deque.offer(temp.right);
                }
            }
            if (!left2right) {
                Collections.reverse(tempRes);
            }
            left2right = !left2right;
            res.add(tempRes);
        }
        return res;
    }
}
